//
//  HistoryPriceRecord.m
//  iStock Global
//
//  Created by Andy on 2010/11/29.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "HistoryPriceRecord.h"
#import "GridTableCell.h"

#define LABEL_MonthDay 1 
#define LABEL_Year 2 
#define LABEL_Open 3 
#define LABEL_Last 4 
#define LABEL_High 5 
#define LABEL_Low 6  
#define LABEL_Vol 7 

@implementation HistoryPriceRecord
@synthesize delegate;
@synthesize delegate_UIPopoverController;

#pragma mark -
#pragma mark Initialization
-(id)initWithTargetSymbol:(DataObject *)adataobject {
	self= [super init];
	if (self) {
		
		
		stockdata=adataobject;
		
		
	}
	return self;	 	
}


- (void)loadView {
	
	UIView *mainview;
	mainview=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
	self.view=mainview;
	[mainview release];
    self.view.backgroundColor=[UIColor lightTextColor];	
	UINavigationBar *nav;
	
	nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];	
	
	UIBarButtonItem *barButton ;
	barButton = [[UIBarButtonItem alloc] initWithTitle:  NSLocalizedString(@"Done",@"完成")  style:UIBarButtonItemStylePlain target:self action:@selector(slideOut)];
	UINavigationItem *item;
	item = [[UINavigationItem alloc] initWithTitle:  NSLocalizedString(@"History Price Data", @"歷史價格詳細資料")  ];
	[item setRightBarButtonItem: barButton];
    [barButton release];
    [nav pushNavigationItem:item animated:NO];	
	[item release];
	
	//UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithTitle:@"GoBack" style:UIBarButtonItemStyleDone target:self action:@selector(slideOut)];
	 
             
    myTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 48.0f, 320.0f, self.view.frame.size.height)  style:UITableViewStylePlain]; 
        
    
	
	StockInformation_0 = [[NSArray arrayWithObjects:
						   @"基本資料", @"歷史價格明細 ",  @"歷史技術指標明細",
						   nil] retain];	
	 
	myTable.dataSource = self;	//連結資料 UITableViewDataSource
	myTable.delegate=self; //連結動作 UITableViewDelegate	
	[self.view addSubview:myTable];
	[myTable release];
	//[self.view addSubview:headerbutton];
	[mainview addSubview:nav ];	
	[nav release];
}

#pragma mark -
#pragma mark  退出至上一層
-(void)slideOut{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
       [delegate_UIPopoverController dismissPopoverAnimated:YES]; 
    }    
	else{
       [self.delegate HistoryPriceRecordDidFinish:self];    
    }
   	
	
    
}
#pragma mark -
#pragma mark Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
	return [stockdata.financialData count];
}
//表格頭標題
//HeaderInSection 增加物件
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	return 30;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(30.0, 0.0, 100.0, 200.0)] autorelease];
  	
	//section 底色設定
	customView.backgroundColor = [UIColor lightGrayColor];
	 
	//alpha 透明度 0.1:幾乎透明 1:不透明
	
	
	UILabel * headerLabel_mmdd = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_mmdd.backgroundColor = [UIColor clearColor];
	headerLabel_mmdd.opaque = NO;
	headerLabel_mmdd.textColor = [UIColor blueColor];
	headerLabel_mmdd.font = [UIFont boldSystemFontOfSize:13];
	headerLabel_mmdd.frame = CGRectMake(10,-10, 100.0, 30.0);
	headerLabel_mmdd.textAlignment = UITextAlignmentLeft;
	headerLabel_mmdd.text = [NSString stringWithString:NSLocalizedString(@"Month/Day", @"月/日") ];
	headerLabel_mmdd.alpha=1;	
	[customView addSubview:headerLabel_mmdd];
	
	UILabel * headerLabel_year = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_year.backgroundColor = [UIColor clearColor];
	headerLabel_year.opaque = NO;
	headerLabel_year.textColor = [UIColor redColor];
	headerLabel_year.font = [UIFont boldSystemFontOfSize:13];
	headerLabel_year.frame = CGRectMake(10,5, 100.0, 30.0);
	headerLabel_year.textAlignment = UITextAlignmentLeft;
	headerLabel_year.text = [NSString stringWithString:NSLocalizedString(@"Year", @"年") ];
	headerLabel_year.alpha=1;	
	[customView addSubview:headerLabel_year];	
	
	UILabel * headerLabel_close = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_close.backgroundColor = [UIColor clearColor];
	headerLabel_close.opaque = NO;
	headerLabel_close.textColor = [UIColor blueColor];
	headerLabel_close.font = [UIFont boldSystemFontOfSize:13];
	headerLabel_close.frame = CGRectMake(100,-10, 100.0, 30.0);
	headerLabel_close.textAlignment = UITextAlignmentLeft;
	headerLabel_close.text = [NSString stringWithString:NSLocalizedString(@"Close", @"收盤價") ];
	headerLabel_close.alpha=1;	
	[customView addSubview:headerLabel_close];			  
	
	UILabel * headerLabel_open = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_open.backgroundColor = [UIColor clearColor];
	headerLabel_open.opaque = NO;
	headerLabel_open.textColor = [UIColor redColor];
	headerLabel_open.font = [UIFont boldSystemFontOfSize:13];
	headerLabel_open.frame = CGRectMake(100,5, 100.0, 30.0);
	headerLabel_open.textAlignment = UITextAlignmentLeft;
	headerLabel_open.text = [NSString stringWithString:NSLocalizedString(@"Open", @"開盤價") ];
	headerLabel_open.alpha=1;	
	[customView addSubview:headerLabel_open];		  
	
	
	UILabel * headerLabel_high = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_high.backgroundColor = [UIColor clearColor];
	headerLabel_high.opaque = NO;
	headerLabel_high.textColor = [UIColor blueColor];
	headerLabel_high.font = [UIFont boldSystemFontOfSize:13];
	headerLabel_high.frame = CGRectMake(180,-10, 100.0, 30.0);
	headerLabel_high.textAlignment = UITextAlignmentLeft;
	headerLabel_high.text = [NSString stringWithString:NSLocalizedString(@"High", @"最高價") ];
	headerLabel_high.alpha=1;	
	[customView addSubview:headerLabel_high];			  
	
	UILabel * headerLabel_low = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_low.backgroundColor = [UIColor clearColor];
	headerLabel_low.opaque = NO;
	headerLabel_low.textColor = [UIColor redColor];
	headerLabel_low.font = [UIFont boldSystemFontOfSize:13];
	headerLabel_low.frame = CGRectMake(180,5, 100.0, 30.0);
	headerLabel_low.textAlignment = UITextAlignmentLeft;
	headerLabel_low.text = [NSString stringWithString:NSLocalizedString(@"Low" ,@"最低價") ];
	headerLabel_low.alpha=1;	
	[customView addSubview:headerLabel_low];		
	
	
	UILabel * headerLabel_vol = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_vol.backgroundColor = [UIColor clearColor];
	headerLabel_vol.opaque = NO;
	headerLabel_vol.textColor = [UIColor blueColor];
	headerLabel_vol.font = [UIFont boldSystemFontOfSize:13];
	headerLabel_vol.frame = CGRectMake(260,-10, 100.0, 30.0);
	headerLabel_vol.textAlignment = UITextAlignmentLeft;
	headerLabel_vol.text = [NSString stringWithString:NSLocalizedString(@"Volume",@"成交量") ];
	headerLabel_vol.alpha=1;	
	[customView addSubview:headerLabel_vol];			
	 	
	
	return customView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
	
	GridTableCell *cell = (GridTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	
	if (cell == nil) {
		//cell = [[[GridTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
		cell=   [[[GridTableCell alloc] initWithFrame:CGRectZero   ] autorelease]   ;
		
		
        NSArray *financialData = stockdata.financialData;
		NSDictionary *fData = (NSDictionary *)[financialData objectAtIndex:[ stockdata.financialData count] -indexPath.row-1];
		
		NSDate *theDate= [fData objectForKey:@"date"];	
		NSDateFormatter* mmdd = [[NSDateFormatter alloc]init];
		[mmdd setDateFormat:@"MM/dd"];
		NSString *smmdd = [mmdd stringFromDate:theDate];
		[mmdd release];
        
        NSDateFormatter* yyyy = [[NSDateFormatter alloc]init];
		[yyyy setDateFormat:@"yyyy"];
		NSString *syyyy = [yyyy stringFromDate:theDate];		
		[yyyy release];
		
		[cell addColumn:80];
		[cell addColumn:160];		
		[cell addColumn:240];		
		[cell addColumn:320];		
		
		UILabel *label = [[[UILabel	alloc] initWithFrame:CGRectMake(0.0, 0,50.0, tableView.rowHeight/2)] autorelease]; 
		label.tag = LABEL_MonthDay; 
		label.font= [UIFont systemFontOfSize:13.0]; 
		label.text=smmdd;
		label.textAlignment = UITextAlignmentRight; 
		label.textColor = [UIColor blueColor]; 
		label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
		 
		UIViewAutoresizingFlexibleHeight; 
		[cell.contentView addSubview:label]; 
		
		label =  [[[UILabel	alloc] initWithFrame:CGRectMake( 0.0, 20,50.0, tableView.rowHeight/2)] autorelease]; 
		label.tag = LABEL_Year; 
		label.font = [UIFont systemFontOfSize:13.0]; 
		 label.text = syyyy; 
		label.textAlignment = UITextAlignmentRight; 
		label.textColor = [UIColor redColor]; 
		label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
		UIViewAutoresizingFlexibleHeight; 
		[cell.contentView addSubview:label]; 
		
		
		
		
		
		label =  [[[UILabel	alloc] initWithFrame:CGRectMake(90.0, 0, 50.0, tableView.rowHeight/2) ] autorelease]; 		
		label.tag = LABEL_Open; 
		label.font = [UIFont systemFontOfSize:13.0]; 
		// add some silly value
		label.text = [NSString stringWithFormat:@"%0.02f", [[fData objectForKey:@"close"] floatValue]];
		label.textAlignment = UITextAlignmentRight; 
		label.textColor = [UIColor blueColor]; 
		label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
		UIViewAutoresizingFlexibleHeight; 
		[cell.contentView addSubview:label];
 		
		
		
		label =  [[[UILabel	alloc] initWithFrame:CGRectMake(90.0, 20, 50.0, tableView.rowHeight/2) ] autorelease]; 		
		label.tag =LABEL_Last; 
		label.font = [UIFont systemFontOfSize:13.0]; 
		// add some silly value
		label.text = [NSString stringWithFormat:@"%0.02f", [[fData objectForKey:@"open"] floatValue]];
		label.textAlignment = UITextAlignmentRight; 
		label.textColor = [UIColor redColor]; 
		label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
		UIViewAutoresizingFlexibleHeight; 
		[cell.contentView addSubview:label];
		
		
		label =  [[[UILabel	alloc] initWithFrame:CGRectMake(170.0, 0, 50.0, tableView.rowHeight/2) ] autorelease]; 		
		label.tag = LABEL_High; 
		label.font = [UIFont systemFontOfSize:13.0]; 
		// add some silly value
		label.text = [NSString stringWithFormat:@"%0.02f", [[fData objectForKey:@"high"] floatValue]];
		label.textAlignment = UITextAlignmentRight; 
		label.textColor = [UIColor blueColor]; 
		label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
		UIViewAutoresizingFlexibleHeight; 
		[cell.contentView addSubview:label];
 		
		
		
		label =  [[[UILabel	alloc] initWithFrame:CGRectMake(170.0, 20, 50.0, tableView.rowHeight/2) ] autorelease]; 		
		label.tag =LABEL_Low; 
		label.font = [UIFont systemFontOfSize:13.0]; 
		// add some silly value
		label.text = [NSString stringWithFormat:@"%0.02f", [[fData objectForKey:@"low"] floatValue]];
		label.textAlignment = UITextAlignmentRight; 
		label.textColor = [UIColor redColor]; 
		label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
		UIViewAutoresizingFlexibleHeight; 
		[cell.contentView addSubview:label];		
		
		
		label =  [[[UILabel	alloc] initWithFrame:CGRectMake(240.0, 0, 70.0, tableView.rowHeight/2) ] autorelease]; 		
		label.tag = LABEL_Vol; 
		label.font = [UIFont systemFontOfSize:11.0]; 
		// add some silly value
		label.text = [NSString stringWithFormat:@"%i", [[fData objectForKey:@"volume"] intValue]];
		label.textAlignment = UITextAlignmentRight; 
		label.textColor = [UIColor blueColor]; 
		label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
		UIViewAutoresizingFlexibleHeight; 
		[cell.contentView addSubview:label];		
		
		
	}
	
	return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

